package com.anolesoft.yr.mbus.part.controller;

import com.anolesoft.common.core.web.domain.AjaxResult;
import com.anolesoft.yr.mbus.part.dto.PartBomDTO;
import com.anolesoft.yr.mbus.part.service.PartBomService;
import com.anolesoft.yr.mbus.part.vo.PartBomVO;
import com.anolesoft.yr.util.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @author zhj
 * @date 2020/8/17 10:29
 */
@RestController
@Deprecated
public class PartBomController {
    @Autowired
    private PartBomService partBomService;

    @PostMapping(value = "/api/part/partbom/add", produces = MediaType.APPLICATION_JSON_VALUE)
    public AjaxResult create(PartBomVO partBomVO) {
        PartBomDTO partBomDTO = vo2dto(partBomVO, true);
        return new AjaxResult(HttpStatus.SUCCESS, Constants.CREATE_SUCCESS_MESSAGE, partBomService.create(partBomDTO));
    }

    private PartBomDTO vo2dto(PartBomVO partBomVO, boolean newId) {
        return PartBomDTO.builder()
                .remark(partBomVO.getRemark())
                .quantity(partBomVO.getQuantity())
                .partModelUid(partBomVO.getPartModelUid())
                .parentPartModelUid(partBomVO.getParentPartModelUid())
                .objuid(newId ? CommonUtils.fastUUID() : partBomVO.getObjuid())
                .modifyUserUid(SecurityUtils.getUserId())
                .modifyTime(partBomVO.getModifyTime())
                .factoryCode(SecurityUtils.getFactoryCode())
                .deleteFlag(partBomVO.getDeleteFlag())
                .createUserUid(SecurityUtils.getUserId())
                .createTime(partBomVO.getCreateTime())
                .build();
    }


    @PutMapping(value = "/api/part/partbom/update", produces = MediaType.APPLICATION_JSON_VALUE)
    public AjaxResult modify(PartBomVO partBomVO) {
        PartBomDTO partBomDTO = vo2dto(partBomVO, false);
        return new AjaxResult(HttpStatus.SUCCESS, Constants.MODIFY_SUCCESS_MESSAGE, partBomService.modify(partBomDTO));
    }

    @DeleteMapping(value = "/api/part/partbom/delete/{id}", produces = MediaType.APPLICATION_JSON_VALUE)
    public AjaxResult remove(@PathVariable("id") String id) {
        return new AjaxResult(HttpStatus.SUCCESS, Constants.DELETE_SUCCESS_MESSAGE, partBomService.remove(id));
    }

    @GetMapping(value = "/api/part/partbom/list/all", produces = MediaType.APPLICATION_JSON_VALUE)
    public AjaxResult selectAll() {
        List<PartBomVO> vos = partBomService.selectAll().stream().map(CustomBeanUtils::partBomDTO2VO).collect(Collectors.toList());
        PageInfo<List<PartBomVO>> pageInfo = new PageInfo<>(vos.size(), vos);
        return new AjaxResult(HttpStatus.SUCCESS, Constants.QUERY_SUCCESS_MESSAGE, pageInfo);
    }

    @GetMapping(value = "/api/part/partbom/list/all/page", produces = MediaType.APPLICATION_JSON_VALUE)
    public AjaxResult selectPageAll(
            @RequestParam("rows") Integer limit,
            @RequestParam("page") Integer curr) {
        PageData<List<PartBomDTO>> pageData = partBomService.selectAll(limit, curr);
        List<PartBomVO> vos = pageData.getItems().stream().map(CustomBeanUtils::partBomDTO2VO).collect(Collectors.toList());
        PageInfo<List<PartBomVO>> pageInfo = new PageInfo<>(limit, pageData.getTotal(), vos);
        return new AjaxResult(HttpStatus.SUCCESS, Constants.QUERY_SUCCESS_MESSAGE, pageInfo);
    }

    @GetMapping(value = "/api/part/partbom/list/condition", produces = MediaType.APPLICATION_JSON_VALUE)
    public AjaxResult selectList(PartBomVO partBomVO) {
        PartBomDTO partBomDTO = vo2dto(partBomVO, false);
        List<PartBomVO> vos = partBomService.selectList(partBomDTO).stream().map(CustomBeanUtils::partBomDTO2VO).collect(Collectors.toList());
        PageInfo<List<PartBomVO>> pageInfo = new PageInfo<>(vos.size(), vos);
        return new AjaxResult(HttpStatus.SUCCESS, Constants.QUERY_SUCCESS_MESSAGE, pageInfo);
    }

    @GetMapping(value = "/api/part/partbom/list/condition/page", produces = MediaType.APPLICATION_JSON_VALUE)
    public AjaxResult selectPageList(PartBomVO partBomVO) {
        PartBomDTO partBomDTO = vo2dto(partBomVO, false);
        Integer limit = partBomVO.getRows();
        Integer curr = partBomVO.getPage();
        PageData<List<PartBomDTO>> pageData = partBomService.selectList(partBomDTO, limit, curr);
        List<PartBomVO> vos = pageData.getItems().stream().map(CustomBeanUtils::partBomDTO2VO).collect(Collectors.toList());
        PageInfo<List<PartBomVO>> pageInfo = new PageInfo<>(limit, pageData.getTotal(), vos);
        return new AjaxResult(HttpStatus.SUCCESS, Constants.QUERY_SUCCESS_MESSAGE, pageInfo);
    }
}
